﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Wicture.DbRESTFul;
using Wicture.QPZS.EPC.API.Domain.Entities;

namespace Wicture.QPZS.EPC.API.InvokeContextBuilders
{
    public class ThrottlingContextBuilderProxy : IThrottlingContextBuilderProxy
    {
        private readonly IThrottlingContextBuilder[] _throttlingContextBuilders;
        private readonly IThrottedContextBuilder[] _throttedContextBuilders;

        public ThrottlingContextBuilderProxy(IThrottlingContextBuilder[] throttlingContextBuilders, IThrottedContextBuilder[] throttedContextBuilders)
        {
            _throttlingContextBuilders = throttlingContextBuilders.OrderBy(s => s.Order).ToArray();
            _throttedContextBuilders = throttedContextBuilders.OrderBy(s => s.Order).ToArray();
        }

        public ThrottlingContext Build(object sender, ApiInvokingArgs e)
        {
            var context = new ThrottlingContext { };
            foreach (var builder in _throttlingContextBuilders)
            {
                builder.Build(context, sender, e);
            }
            return context;
        }

        public void Build(ThrottlingContext context, object sender, ApiInvokedArgs e)
        {
            foreach (var builder in _throttedContextBuilders)
            {
                builder.Build(context, sender, e);
            }
        }
    }
}
